
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 注册通道api
 */
 
@Api(tags = "Register",description = "注册通道")
@RestController
public class RegisterController {
    
    @Autowired
    private RegisterDao dao;
   
    @Autowired
    private PropertyDao pdao;
    
    @Autowired
    private DocumentTypeDao ddao;

    /**
     * 获取注册通道集合
     * @return api结果
     */
    @ApiOperation(value = "获取注册通道集合")
    @RequestMapping(value =  "/api/Register/",method = {RequestMethod.GET})
    public ApiResult<List<Register>> Get(){
        ApiResult<List<Register>> result=new ApiResult<List<Register>>();
        try 
        {
            List<Register> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取注册集合
     * @param id 项目ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取注册集合")
    @RequestMapping(value =  "/api/Register/Project/{projectId}",method = {RequestMethod.GET})
    public ApiResult<List< Register>> Project(@PathVariable String projectId){
        ApiResult<List<Register>> result=new ApiResult<List<Register>>();
        try
        {
            List<Register> model= dao.findByProjectId(projectId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取注册通道信息
     * @param id 注册通道ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取注册通道信息")
    @RequestMapping(value =  "/api/Register/{id}",method = {RequestMethod.GET})
    public ApiResult<Register> Get(@PathVariable Long id){
        ApiResult<Register> result=new ApiResult<Register>();
        try
        {
            Register model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改注册通道信息
     * @param id 注册通道ID
     * @param model 注册通道信息
     * @return api结果
     */
    @ApiOperation(value = "修改注册通道信息")
    @RequestMapping(value =  "/api/Register/{id}",method = {RequestMethod.PUT})
    public ApiResult<Register> Edit(@PathVariable Long id,@RequestBody Register model, HttpServletRequest request){
        ApiResult<Register> result=new ApiResult<Register>();
        try
        {
            if(model.getProjectId()==null||model.getProjectId().length()==0){
                result.setHasError(true);
                result.setMessage("项目ID不能为空");
            }else{
                model.setRegisterId(id);
                model.setCreateDate(new Date());

                if(request.getServerPort()!=80){
                    model.setUrl(request.getScheme() + "://" + request.getServerName()+":"+request.getServerPort()+"/login/"+model.getRegisterId());
                }else{
                    model.setUrl(request.getScheme() + "://" + request.getServerName()+"/login/"+model.getRegisterId());
                }
                
                if(model.getDocumentTypeId()!=null){
                    DocumentType type=ddao.getOne(model.getDocumentTypeId());

                    type.setName(model.getName());

                    ddao.save(type);
                }else{
                    DocumentType type=new DocumentType();
                    
                    type.setProjectId(model.getProjectId());
                    type.setCreateDate(new Date());
                    type.setName(model.getName());

                    ddao.save(type);
                    
                    model.setDocumentTypeId(type.getDocumentTypeId());
                }

                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加注册通道信息
     * @param model 注册通道信息
     * @return api结果
     */
    @ApiOperation(value = "添加注册通道信息")
    @RequestMapping(value =  "/api/Register/",method = {RequestMethod.POST})
    public ApiResult<Register> Add(@RequestBody Register model, HttpServletRequest request){
         ApiResult<Register> result=new ApiResult<Register>();
        try
        {
            if(dao.countByProjectIdAndName(model.getProjectId(),model.getName())>0){
                result.setHasError(true);
                result.setMessage("注册通道名称重复");
            }else if(ddao.countByProjectIdAndName(model.getProjectId(),model.getName())>0){
                result.setHasError(true);
                result.setMessage("证件名称相同");
            }
            else{
                DocumentType type=new DocumentType();
    
                type.setCreateDate(new Date());
                type.setProjectId(model.getProjectId());
                type.setName(model.getName());
                
                ddao.save(type);
    
                result.setData(model);

                model.setDocumentTypeId(type.getDocumentTypeId());
                model.setCreateDate(new Date());
                dao.save(model);
    
                if(request.getServerPort()!=80){
                    model.setUrl(request.getScheme() + "://" + request.getServerName()+":"+request.getServerPort()+"/login/"+model.getRegisterId());
                }else{
                    model.setUrl(request.getScheme() + "://" + request.getServerName()+"/login/"+model.getRegisterId());
                }
                dao.save(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除注册通道信息
     * @param id 注册通道ID
     * @return api结果
     */
    @ApiOperation(value = "删除注册通道信息")
    @RequestMapping(value =  "/api/Register/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            pdao.deleteByRegisterId(id);
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

